package com.chery.media.library.extend

import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils

/**
 * Created by Beetle_Sxy on 11/30/20.
 * 带文案说明的权限申请提示框
 */
//CALENDAR, CAMERA, CONTACTS, LOCATION, MICROPHONE, PHONE, SENSORS, SMS, STORAGE,


fun explainPermissionsSuccess(
	@PermissionConstants.PermissionGroup vararg permissions: String,
	msg: String? = null,
	onCallback: () -> Unit,
) {
	explainPermissions(*permissions) { if (it) onCallback() else msg?.toast() }
}

fun explainPermissions(
	@PermissionConstants.PermissionGroup vararg permissions: String, onCallback: (Boolean) -> Unit,
) {
	if (PermissionUtils.isGranted(*permissions)) {
		onCallback(true)
		return
	}
	
	PermissionUtils.permission(*permissions)
		.explain { activity, denied, shouldRequest ->
			shouldRequest.start(true)
		}.callback { isAllGranted, granted, deniedForever, denied ->
			if (deniedForever.isNotEmpty()) PermissionUtils.launchAppDetailsSettings()
			else onCallback(denied.isEmpty() && deniedForever.isEmpty())
		}.request()
}

